﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
    public partial class KiesKlasForm : Form
    {

        private SqlConnection conn = null;
        private String connString = "";
        private SqlCommand cmd = null;
        public Database dbActies;
        public AanmaakForm1 aanmaak;

        public KiesKlasForm(ref Database dbActies)
        {
            this.dbActies = dbActies;            
            InitializeComponent();
            connString = Properties.Settings.Default.WiskundeOefeningenConnectionString;
            conn = new SqlConnection(connString);
        }

        private void KiesKlasForm_Load(object sender, EventArgs e)
        {
            
            String cmdString = ("SELECT DISTINCT klas FROM Leerling");
            cmd = new SqlCommand(cmdString, conn);
            conn.Open();
            SqlDataReader myReader;
            myReader = cmd.ExecuteReader();
            while (myReader.Read())
            {

                cmbKlas.Items.Add(myReader.GetString(0).ToString());
            }
            myReader.Close();
            conn.Close();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Hide();
        }


        private void cmbKlas_SelectedIndexChanged(object sender, EventArgs e)
        {            
            string klas = cmbKlas.SelectedItem.ToString();            
            klas = klas.Trim();                     
            leerlingTableAdapter1.Fill(wiskundeOefeningenDataSet.Leerling);
            leerlingBindingSource1.Filter = "klas='" + klas + "'";
        }

        private void btnKiesSelectie_Click(object sender, EventArgs e)
        {            
            string klas = cmbKlas.SelectedItem.ToString();             
            DataTable tabel = leerlingTableAdapter1.GetDataByKlas(klas);
            for (int i = 0; i < tabel.Rows.Count; i++)
            {
                object[] list =  tabel.Rows[i].ItemArray.ToArray();              
                string leerling = list[0].ToString();
                string leerlingNaam = list[1].ToString();                
                dbActies.Leerlingen.Add(leerling);
                dbActies.NamenLln.Add(leerlingNaam);
            }            
            aanmaak.gekozen();
            this.Close();
        }       
    }    
}
